package me.numark;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.numark.model.News;
import me.numark.utils.Constants;

/**
 * Created by tian on 14/11/16:下午10:41.
 */
public class ArticleActivity extends Activity implements View.OnClickListener {

    @InjectView(R.id.webview)
    WebView webview;

    @InjectView(R.id.recommend)
    View recommendButton;

    @InjectView(R.id.total_recommends)
    TextView totalRecommends;

    @InjectView(R.id.title)
    TextView titleText;

    @InjectView(R.id.back)
    View backArrow;

    private News mNews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);
        ButterKnife.inject(this);
        try {
            mNews = new Gson()
                    .fromJson(getIntent().getStringExtra(Constants.NEWS_EXTRA), News.class);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
            finish();
        }
        int commentCount = getIntent().getIntExtra(Constants.SHARE_COUNT, 0);
        totalRecommends.setText(Integer.toString(commentCount));
        totalRecommends.setOnClickListener(this);
        titleText.setText(getString(R.string.article_title, mNews.by_user.nick));
        webview.loadUrl(mNews.url);
        recommendButton.setOnClickListener(this);
        backArrow.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recommend:
                Intent intent = new Intent(this, RecommendActivity.class);
                intent.putExtra(Constants.NEWS_EXTRA, mNews.toString());
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
            case R.id.total_recommends:
                intent = new Intent(this, ShareListActivity.class);
                intent.putExtra(Constants.NEWS_EXTRA, mNews.toString());
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
